﻿using System.Web;
using Microsoft.AspNetCore.Authorization;
using Simple.Services;

namespace Simple.WebApi.Controllers.System
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [Route("api/Parent/[action]")]
    [ApiController]
    [Authorize]
    public class ParentController : ControllerBase
    {
        private readonly ParentService _parentService;

        public ParentController(ParentService parentService)
        {
            _parentService = parentService;
        }

        /// <summary>
        /// 家长列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> List()
        {
            List<ParentModel> data = await _parentService.GetAsync();
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 家长列表(班级id)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> ListByClassId(Guid classId)
        {
            List<ParentModel> data = await _parentService.GetAsync(classId);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 家长查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> Page([FromQuery] ParentPageInputModel model)
        {
            PageResultModel<ParentModel> data = await _parentService.GetPageAsync(model);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 家长增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Add([FromBody] ParentInputModel model)
        {
            await _parentService.AddAsync(model);
            return AppResult.Status200OK("新增成功");
        }

        /// <summary>
        /// 家长编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Edit([FromBody] ParentInputModel model)
        {
            await _parentService.UpdateAsync(model);
            return AppResult.Status200OK("更新成功");
        }

        /// <summary>
        /// 家长删除
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Delete([FromBody] List<IdInputModel> models)
        {
            await _parentService.DeleteAsync(models.Select(m => m.Id));
            return AppResult.Status200OK("删除成功");
        }

        /// <summary>
        /// 开启/关闭微信端
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> OpenOrCloseWeChat([FromBody] WeChatQueryModel model)
        {
            await _parentService.OpenOrCloseWeChat(model.PhoneNumber);
            return AppResult.Status200OK("修改成功");
        }

        /// <summary>
        /// 批量开启/关闭微信端
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> BatchOpenOrCloseWeChat([FromBody] IdsInputModel models)
        {
            await _parentService.BatchOpenOrCloseWeChat(models);
            return AppResult.Status200OK("修改成功");
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Reset([FromBody] IdInputModel models)
        {
            await _parentService.Reset(models.Id);
            return AppResult.Status200OK("重置成功,默认密码为'123456'");
        }

        /// <summary>
        /// 修改个人信息包括头像（移动端）
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> UpdatePersonalInfo([FromBody] UpdatePersonalInfoInput models)
        {
            await _parentService.UpdatePersonalInfo(models);
            return AppResult.Status200OK("修改成功");
        }

        /// <summary>
        /// 获取个人信息(移动端)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<AppResult> GetPersonalInfo([FromQuery] AppParentQuery model)
        {
            var data = await _parentService.GetPersonalInfoAsync(model);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 幼儿没有上传头像的家长
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<FileContentResult> ExportChildren()
        {
            var file = await _parentService.NoImgParent();
            return File(file.FileData, "application/octet-stream", fileDownloadName: HttpUtility.UrlEncode(file.FileName));
        }
    }
}
